<?php

namespace app\common\service;

use think\facade\Cache;

use app\common\service\storage\engine\Aliyun;

/*
 * 小说详情获取
 * */

class ContentService
{

    const REDIS_BOOK_CHAPTER_KEY = 'redis_book_chapter_%u_%u';


    /**
     * @notes 上传小说内容到云服务器
     * @param int $book_id 书籍ID
     * @param int $chapter_id 章节ID
     * @param string $content 章节ID
     * @return bool
     * @author 段誉
     * @date 2021/12/28 15:19
     * @remark
     */
    public static function uploadContent(int $book_id, int $chapter_id, string $content)
    {
        $config  = [
            'access_key' => config('oss.oss.access_key'),
            'secret_key' => config('oss.oss.secret_key'),
            'bucket'     => config('oss.oss.bucket'),
            'domain'     => config('oss.oss.domain'),
        ];
        $aliyun  = new Aliyun($config);
        $saveDir = 'content/' . $book_id . '/' . $chapter_id . '.txt';
        $result  = $aliyun->uploadContent($saveDir, $content);
        return $saveDir;
    }

    /**
     * @notes 获取小说内容链接
     * @param string $content_url 内容链接
     * @return string
     * @author 段誉
     * @date 2021/12/28 15:19
     * @remark
     */
    public static function getContentUrl(string $content_url, $bookId, $chapterId)
    {

        if ($content = self::getCacheContent($bookId, $chapterId)) {

            return $content;
        }

        $config     = [
            'access_key' => config('oss.oss.access_key'),
            'secret_key' => config('oss.oss.secret_key'),
            'bucket'     => config('oss.oss.bucket'),
            'domain'     => config('oss.oss.domain'),
        ];
        $aliyun     = new Aliyun($config);
        $privateUrl = $aliyun->getContentUrl($content_url);
        $content    = file_get_contents($privateUrl);

        self::setCacheContent($content, $bookId, $chapterId);

        return $content;
    }

    /**
     * @notes 获取小说内容链接
     * @param string $coverUrl 内容链接
     * @return string
     * @author 段誉
     * @date 2021/12/28 15:19
     * @remark
     */
    public static function uploadByUrl(string $coverUrl)
    {
        $config    = [
            'access_key' => config('oss.oss.access_key'),
            'secret_key' => config('oss.oss.secret_key'),
            'bucket'     => config('oss.oss.static_bucket'),
            'domain'     => config('oss.oss.static_domain'),
        ];
        $aliyun    = new Aliyun($config);
        $extension = pathinfo($coverUrl, PATHINFO_EXTENSION);
        $fileName  = date('YmdHis') . substr(md5($coverUrl), 0, 5)
            . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . '.' . $extension;
        $saveDir   = 'uploads/images' . '/' . date('Ymd') . '/' . $fileName;
        $result    = $aliyun->uploadByUrl($saveDir, $coverUrl, $fileName);
        if ($result) {
            return $saveDir;
        } else {
            return false;
        }
    }


    /**
     * 缓存章节内容
     *
     * @param $content
     * @param $bookId
     * @param $chapterId
     * @return bool
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    private static function setCacheContent($content, $bookId, $chapterId)
    {
        $cache = Cache::store('redis');
        $cache->handler()->select(1);

        return $cache->set(
            sprintf(self::REDIS_BOOK_CHAPTER_KEY, $bookId, $chapterId),
            bzcompress($content)
        );
    }


    /**
     * 获取缓存章节内容
     *
     * @param $bookId
     * @param $chapterId
     * @return mixed
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    private static function getCacheContent($bookId, $chapterId)
    {
        $cache = Cache::store('redis');
        $cache->handler()->select(1);

        return bzdecompress($cache->get(sprintf(self::REDIS_BOOK_CHAPTER_KEY, $bookId, $chapterId)));
    }

    /**
     * 删除章节内容
     *
     * @param $content
     * @param $bookId
     * @param $chapterId
     * @return bool
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public static function delCacheContent($bookId, $chapterId)
    {
        $cache = Cache::store('redis');
        $cache->handler()->select(1);

        return $cache->delete(
            sprintf(self::REDIS_BOOK_CHAPTER_KEY, $bookId, $chapterId)
        );
    }
}
